<template>
  <div class="app-container">
    <el-form
      ref="salaryInfoForm"
      :model="listQuery"
      label-position="right"
      label-width="140px"
      size="small"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item
            label="工资月份："
            prop="工资月份"
          >
            <el-date-picker
              v-model="listQuery.salaryMonth"
              type="month"
              placeholder="选择月"
              format="yyyy-MM"
              value-format="yyyy-MM"
              :clearable="false"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="工资卡开户银行："
            prop="工资卡开户银行"
          >
            <el-select
              v-model="listQuery.bankCode"
              class="filter-item"
              placeholder="--请选择--"
              clearable
            >
              <el-option
                v-for="item in agencyBankLists"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-button
          v-if="false"
          type="primary"
          @click="handleQuery"
        >查询</el-button>
      </el-row>
    </el-form>
    <el-table
      v-loading="listLoading"
      :cell-style="cellStyle"
      :stripe="true"
      :data="list"
      style="width: 100%"
      max-height="450px"
      fit
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        label="序号"
        type="index"
        :width="indexWidth"
      />
      <el-table-column
        v-for="(val, i) in tableHead"
        :key="i"
        :label="val"
      >
        <template slot-scope="{row}">
          <div class="text-center">
            <span v-if="i <=1 "> {{ row[val] }}</span>
            <span v-else-if="val === '人数'"> {{ row[val] }}</span>
            <span v-else style="float:right"> {{ Number(row[val]).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,") }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { vwSalAgencyPersonTrafficAllowance } from '@/api/reportForm'
import { getAgencyBank } from '@/api/baseInfo'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
export default {
  components: {
    Pagination,
    tableEmpty
  },
  data() {
    return {
      list: [],
      listLoading: true,
      total: 0,
      indexWidth: '50px',
      listQuery: {
        page: 1,
        limit: 200,
        salaryMonth: '',
        bankCode: ''
      },
      tableHead: [],
      agencyBankLists: []
    }
  },
  watch: {
    listQuery: {
      handler() {
        this.handleQuery()
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    this.listQuery.page = 1
    this.getCurrenMonth()
    this.getList()
    getAgencyBank().then(response => {
      this.agencyBankLists = Object.assign({}, response.data)
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      vwSalAgencyPersonTrafficAllowance(this.listQuery).then(response => {
       
        this.tableHead = response.data.tableHeads
        this.list = response.data.rows
        this.total = response.data.total
        if (this.total === 0) {
          this.indexWidth = '200px'
        } else {
          this.indexWidth = '50px'
        }
        this.listLoading = false
      })
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 1) {
        // 指定列号
        return 'text-align:left'
      } else {
        return ''
      }
    },
    handleQuery() {
      this.listQuery.page = 1
      // this.listQuery.limit = 10
      this.getList()
    },
    getCurrenMonth() {
      const date = new Date()
      const month = (date.getMonth() + 1).toString().padStart(2, '0')
      const year = date.getFullYear()
      this.listQuery.salaryMonth = year + '-' + month
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep.el-table td {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: center;
}
::v-deep.el-table th {
  text-align: center;
}
</style>
